<?php
    class EvaluacionesController extends AppController {
        var $name = 'Evaluaciones';
        var $uses = array('Evaluacion');
        var $helpers = array('Javascript');
        var $components = array('AdminParameters');        

        function evaluacionesAntropometricas(){
            $this->render("evaluaciones_antropometricas", "ajax");
        }

        function  getList(){
            $this->render("list", "ajax");
        }

        function getListData($page=1, $limit=10, $orderColumn='fecha', $ascDesc='desc'){

            $model = "Evaluacion.";

            $fields = array(
                $model."id", $model."fecha", $model."peso", $model."talla", 
                $model."perimetro_cintura_rc", $model."porcentaje_masa_grasa",
                $model."porcentaje_masa_grasa"
            );

            $result = $this->AdminParameters->getListData($this->Evaluacion, $fields, $this->params["form"], $page, $limit, $orderColumn, $ascDesc);

            $total = 0;
            if($ascDesc == "asc"){
               for($i=0; $i<sizeof($result["data"]); $i++){
                    $this->_agregarCampos(&$result, &$i, &$total, 1);
                }
            }else{
                for($i=count($result["data"])-1; $i>=0; $i--){
                    $this->_agregarCampos(&$result, &$i, &$total, -1);
                }
            }


            $this->set("result", $result);

            $this->render("/elements/ajax_return", "ajax");

        }

        function _agregarCampos($result, $i, $total, $ascDesc){

            $actual =& $result["data"][$i]["Evaluacion"];

            if(isset($result["data"][$i-$ascDesc])){
                $anterior =& $result["data"][$i-$ascDesc]["Evaluacion"];
            }else{
                $anterior =& $result["data"][$i]["Evaluacion"];
            }

            $diferencia  = ($actual["peso"] - $anterior["peso"]);
            $total      += $diferencia;

            if($actual["talla"] == 0){
                $imc = 0;
            }else{
                $imc = $actual["peso"] / pow(0.01*$actual["talla"], 2);
            }

            $actual["diferencia"] = $diferencia;
            $actual["diferencia_acumulada"] = $total;
            $actual["imc"] = round($imc, 2);
        }

        function getDetail($op, $id){

            $this->set("operation", $op);
            $this->set("editId", $id);

            $this->render("detalle_evaluaciones", "ajax");

        }

        function getDetailData($id){
            $data = $this->Evaluacion->find("all", array("conditions" => array("Evaluacion.id" => $id), "recursive" => 0));

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

        function save(){

            if($this->AdminParameters->doSave($this->Evaluacion, $this->params["form"])){
                $this->set("result", array("result" => true));
            }else{
                $this->set("result", array("result" => false));
                $this->set("result", array("error" => "Error while saving data."));
            }


            $this->render("/elements/ajax_return", "ajax");
        }

        function getOptions(){
            $data = $this->Evaluacion->find("list", array("fields" => array("Evaluacion.id", "Evaluacion.fecha")));

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

        function getPesosPaciente($id){

            $data = $this->Evaluacion->find(
                    "all",
                    array(
                        "fields" => array("Evaluacion.id", "Evaluacion.fecha", "Evaluacion.peso", "Evaluacion.peso_ideal"),
                            "conditions" => array(
                                "Evaluacion.paciente_id" => $id
                            ),
                            "order" => array("Evaluacion.fecha DESC")
                        )
             );

            $this->set("result", $data);

            $this->render("/elements/ajax_return", "ajax");
        }
    }
?>